home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -in_the_mag- / program_perfection / lines.c < prev    next >
C/C++ Source or Header  |  1999-12-08  |  3KB  |  147 lines

  1. /*
  2.  * lines.c
  3.  *
  4.  * Line class
  5.  *
  6.  * $Id :$
  7.  * $Log:$
  8.  *
  9.  */
  10.  
  11. #include "defs.h"
  12. #include "lines.h"
  13. #include "memory.h"
  14. #include "errors.h"
  15.  
  16. /*
  17.  * Line_New()
  18.  *
  19.  * PURPOSE: constructor for Line class
  20.  *
  21.  * INPUTS : string - ptr to sequence of chars to use as contents of this
  22.  *                   line. A copy is not mode, so the source string must
  23.  *                   exist as long as the Line object.
  24.  *
  25.  *          len - number of characters in this line. The source string is
  26.  *                not necessarily NULL terminated - len specifies the end
  27.  *                of the string. This is so we can slice up a read-only
  28.  *                buffer into a series of lines
  29.  *
  30.  * RETURNS: if all goes well, a ptr to a freshly allocated Line with the
  31.  *          contents of your choosing.Otherwise NULL.
  32.  *          Possible causes of failure include lack of memory or trying to
  33.  *          exceed the maximum line length (MAX_LINE_LEN). We will need to
  34.  *          differentiate these two eventually . . .
  35.  */
  36.  
  37. LINE_PTR
  38. Line_New( STRPTR string, LINE_LEN_TYPE len, ULONG *err_code )
  39. {
  40.     LINE_PTR new_line;
  41.  
  42.     if( len > MAX_LINE_LEN )
  43.     {
  44.         /* line too long */
  45.         *err_code = ERR_LINE_TOO_LONG;
  46.         return NULL;
  47.     }
  48.  
  49.     if( new_line = (LINE_PTR) Memory_Alloc( sizeof( LINE_TYPE ) ) )
  50.     {
  51.         new_line->Contents = string;
  52.         new_line->Length = len;
  53.     }
  54.     else
  55.         *err_code = ERR_NO_MEMORY;
  56.  
  57.     return new_line;
  58. }
  59.  
  60.  
  61. /*
  62.  * Line_Dispose()
  63.  *
  64.  * PURPOSE: destructor for Line class.
  65.  *
  66.  * INPUTS:  line - the Line object to kill.
  67.  *
  68.  * RETURNS: None
  69.  */
  70.  
  71. VOID
  72. Line_Dispose( LINE_PTR line )
  73. {
  74.     if( line )
  75.         Memory_Free( line, sizeof( LINE_TYPE ) );
  76. }
  77.  
  78.  
  79.  
  80. /*
  81.  * Line_GetContents()
  82.  *
  83.  * PURPOSE: Get a pointer to the contents of a Line object
  84.  *
  85.  * INPUTS : line - the Line to find the contents of.
  86.  *
  87.  * RETURNS: a pointer to the sequence of chars that make up this line.
  88.  *          Although this is returned as a STRPTR, it must be noted
  89.  *          that the 'string' is not necessarily NULL terminated.
  90.  *          Use with care!
  91.  */
  92.  
  93. STRPTR
  94. Line_GetContents( LINE_PTR line )
  95. {
  96.     return line->Contents;
  97. }
  98.  
  99.  
  100. /*
  101.  * Line_GetLength()
  102.  *
  103.  * PURPOSE: Find the length in chars of a Line.
  104.  *
  105.  * INPUTS : line - the Line to find the length of.
  106.  *
  107.  * RETURNS: the length of the desired line.
  108.  */
  109.  
  110.  
  111. LINE_LEN_TYPE
  112. Line_GetLength( LINE_PTR line )
  113. {
  114.     return line->Length;
  115. }
  116.  
  117.  
  118. #ifdef DEBUG
  119.  
  120. #include <dos/dos.h>
  121. #include <proto/dos.h>
  122.  
  123. /*
  124.  * Line_FPuts()
  125.  *
  126.  * PURPOSE: Dump the contents of a line to a file. This is provided
  127.  *          for debugging/testing purposes since Line contents may not
  128.  *          be NULL terminated so cannot just be printed with a straight
  129.  *          FPuts() or FPrintf().
  130.  *
  131.  * INPUTS:  line - the line in question.
  132.  *
  133.  * RETURNS: None.
  134.  */
  135.  
  136. VOID
  137. Line_FPuts( BPTR file, LINE_PTR line )
  138. {
  139.     register LINE_LEN_TYPE i      = line->Length;
  140.     register STRPTR        string = line->Contents;
  141.  
  142.     for( ; *string && i ; string++, i-- )
  143.         FPutC( file, *string );
  144. }
  145.  
  146. #endif /* DEBUG */
  147.